{% extends 'generic/object_create.html' %}
{% load static %}
{% load form_helpers %}

{% block form %}
    <div class="card">
        <div class="card-header"><strong>Approval Workflow Definition</strong></div>
        <div class="card-body">
            {% render_field form.name %}
            {% render_field form.model_content_type %}
            {% render_field form.model_constraints %}
            {% render_field form.weight %}
        </div>
    </div>
    <div class="card">
        <div class="card-header"><strong>Approval Workflow Stage Definitions</strong></div>
        <div class="card-body overflow-auto">
            <table class="table" id="approval-workflow-stages">
                {{ stages.management_form }}
                {% for stage in stages.forms %}
                    {% if forloop.first %}
                        <thead>
                            <tr>
                                {% for field in stage.visible_fields %}
                                    <th>
                                        <span data-bs-toggle="tooltip" data-bs-placement="top" title="{{ field.help_text }}">{{ field.label|capfirst }}</span>
                                    </th>
                                {% endfor %}
                            </tr>
                        </thead>
                    {% endif %}
                    <tr class="formset_row-{{ stages.prefix }}">
                        {% for field in stage.visible_fields %}
                            <td>
                                {# Include the hidden fields in the form #}
                                {% if forloop.first %}
                                    {% for hidden in stage.hidden_fields %}
                                        {{ hidden }}
                                    {% endfor %}
                                {% endif %}
                                {{ field }}
                                {% if field.errors %}
                                    <ul>
                                        {% for error in field.errors %}
                                            {# Embed an HTML comment indicating the error for extraction by tests #}
                                            <!-- FORM-ERROR {{ field.name }}: {{ error }} -->
                                            <li class="text-danger">{{ error }}</li>
                                        {% endfor %}
                                    </ul>
                                {% endif %}
                            </td>
                        {% endfor %}
                    </tr>
                {% endfor %}
            </table>
        </div>
    </div>
    {% include 'inc/extras_features_edit_form_fields.html' %}
{% endblock %}

{% block javascript %}
    {{ block.super }}
    <script src="{% static 'jquery/jquery.formset.js' %}"></script>
    <script type="text/javascript">
        const slugify_prefer_underscores = true;
        $('.formset_row-{{ stages.prefix }}').formset({
            addText: '<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add another Approval Workflow Stage',
            addCssClass: 'btn btn-primary add-row',
            deleteText: '<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span>',
            deleteCssClass: 'btn btn-danger delete-row',
            prefix: '{{ stages.prefix }}',
            formCssClass: 'dynamic-formset-{{ stages.prefix }}',
            keepFieldValues: 'input[type="number"]',
            added: jsify_form
        });
    </script>
{% endblock javascript %}
